﻿using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using TSOL.FastSPWeb.Core;
using TSOL.FastSPWeb.ValueObjects;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Web;
using System.Threading;

namespace TSOL.FastSPWeb.Layouts.TSOL.FastSPWeb
{
    public partial class TSOLFastSPWebStatus : LayoutsPageBase
    {
        protected override bool AllowAnonymousAccess
        {
            get
            {
                return false;
            }
        }

        protected override bool AllowNullWeb
        {
            get
            {
                return false;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!SPContext.Current.Web.CurrentUser.IsSiteAdmin)
                SPUtility.Redirect("AccessDenied.aspx", SPRedirectFlags.Static | SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current);

            base.OnLoad(e);
        }

        protected override void  CreateChildControls()
        {
            if (FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue != null)
            {
                if (FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.WebsCacheDictionary != null)
                    Label1.Text = FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.WebsCacheDictionary.Count.ToString();
                if (FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.taskQ != null)
                    Label2.Text = FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.taskQ.Count.ToString();


                Label4.Text = FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.WaitingForThread.ToString();

                ux_NumOfStartedThreads.Text = FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.workers.Count(a => a.ThreadState == ThreadState.WaitSleepJoin).ToString();
                ux_NumOfRunningThreads.Text = FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.workers.Count(a => a.ThreadState == ThreadState.Running).ToString();

                TextBox1.Text = FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.MaxCopiesOfElevatedWeb.ToString();
                TextBox2.Text = FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.StartedThreads.ToString();
                lock (FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.WebsCacheDictionary)
                {
                    foreach (SPWebCacheCollection _entry in FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.WebsCacheDictionary.Values)
                    {
                        int freeObjects = _entry.Count(a => a.Free == true);
                        int totalObjects = _entry.Count();
                        
                        CachedWebObjects.Items.Add(new ListItem("#" + freeObjects.ToString() + " free, of total#" + totalObjects.ToString() + ", WebID = " + _entry.WebGuid.ToString()));
                    }
                }

                lock (FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.ItemsCacheDictionary)
                {
                    foreach (SPItemsCacheCollection _entry in FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.ItemsCacheDictionary.Values)
                    {
                        if (_entry != null)
                        {
                            int freeObjects = _entry.Count(a => a.Free == true);
                            int totalObjects = _entry.Count();

                            CachedListCollectionObjects.Items.Add(new ListItem("#" + freeObjects.ToString() + " free, of total#" + totalObjects.ToString() + ", WebID = " + _entry.ListGuid.ToString()));
                        }
                    }
                }

                lock (FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.ListsCacheDictionary)
                {
                    foreach (SPListsCacheCollection _entry in FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.ListsCacheDictionary.Values)
                    {
                        if (_entry != null)
                        {
                            int freeObjects = _entry.Count(a => a.Free == true);
                            int totalObjects = _entry.Count();

                            CachedListsObjects.Items.Add(new ListItem("#" + freeObjects.ToString() + " free, of total#" + totalObjects.ToString() + ", WebID = " + _entry.WebGuid.ToString()));
                        }
                    }
                }
             	base.CreateChildControls();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.MaxCopiesOfElevatedWeb = Convert.ToInt32(TextBox1.Text);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.StartThreads(Convert.ToInt32(TextBox2.Text));
            TextBox2.Text = FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.StartedThreads.ToString();
        }
    }
}
